package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.example.demo.service.LogService;
import com.example.demo.service.UserService;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private LogService logService;

    /**
     * 编程式的事务
     * @param userInfo
     * @return
     */
    @RequestMapping("/add")
    public int add(UserInfo userInfo) {       //编程式事务
        //非空校验
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
        || !StringUtils.hasLength(userInfo.getPassword()) ) {
            return 0;
        }
        userInfo.setCreatetime(LocalDateTime.now().toString());
        userInfo.setUpdatetime(LocalDateTime.now().toString());
        //1.开始事务
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);  //返回一个事务状态
        int result = userService.add(userInfo);
        //回滚事务
        //transactionManager.rollback(transactionStatus);

        //提交事务
        transactionManager.commit(transactionStatus);

        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)   //声明式的事务(自动提交)(如果方法执行期间出现异常,将会出现事务回滚)
    @RequestMapping("/insert")
    public Integer insert(UserInfo userInfo) {
        //非空校验
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword()) ) {
            return 0;
        }
        userInfo.setCreatetime(LocalDateTime.now().toString());
        userInfo.setUpdatetime(LocalDateTime.now().toString());
        int result = userService.add(userInfo);
        System.out.println("添加"+result);
        //int n = 10/0;     //搞一个算数异常测试出现异常是否会事务回滚

        if(result > 0) {
            logService.add();         //测试事务传播机制(上面一个事务成功,下一个事务出现异常,观察是否会全部回滚)
        }

//        try {                  //声明式事务(当处理异常后,则不会回滚,这是一个问题)  (解决问题有两种1.将异常抛出去 2.手动代码回滚)
//            int a = 10 / 0;
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//            //throw e;                //抛出异常就会回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();     //代码回滚
//        }
        return result;
    }


}
